//
//  QuickEditCategoryViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 08.05.25.
//

import Foundation
class QuickEditCategoryViewModel : KoiAccountingBaseViewModel {
    
    // 支出类别集合
    @Published var expenseList = Array<AccountingCategory>()
    // 收入类别集合
    @Published var incomeList = Array<AccountingCategory>()
    // 选中类别
    @Published var selectedCategory: AccountingCategory? = nil
    // 选中的子类别
    @Published var selectedSubCategory: AccountingCategory? = nil
    // 选中类别的子类别
    @Published var subCategoryList = Array<AccountingCategory>()
    // 二级类别是否处于展开状态
    @Published var isExpand = false
    // 类别数据访问层
    private let categoryDao = KoiAccountingDB.standard.getCategoryDao()
    
    func loadCategorys(selectedCategory: AccountingCategory?, selectedBookId: String? = nil) {
        var bookId = self.bookId
        if selectedBookId != nil {
            bookId = selectedBookId!
        }
        print("loadCategorys \(selectedCategory?.name)")
        DispatchQueue.global(qos: .background).async {
            // 一级类别，还是二级类别
            var firstCategory: AccountingCategory? = nil
            var subCategory: AccountingCategory? = nil
            var subCategoryList: [AccountingCategory] = [AccountingCategory]()
            if let category = selectedCategory {
                if category.isSubCategory() {
                    let parentId = category.parentId ?? ""
                    let parentCategory = self.categoryDao.getCategoryByCid(cid: parentId, uid: self.uid, bookId: bookId)
                    firstCategory = parentCategory
                    subCategory = category
                    subCategoryList = self.categoryDao.getSubCategoryListByParentId(uid: self.uid, bookId: bookId, parentId: parentId)
                } else {
                    firstCategory = selectedCategory
                }
            }
            let expenseResult = self.categoryDao.getCategoryByType(uid: self.uid, bookId: bookId, type: CategoryType.expense.rawValue)
            let incomeResult = self.categoryDao.getCategoryByType(uid: self.uid, bookId: bookId, type: CategoryType.income.rawValue)
            DispatchQueue.main.async {
                self.selectedCategory = firstCategory
                self.selectedSubCategory = subCategory
                self.expenseList = expenseResult
                self.incomeList = incomeResult
                self.subCategoryList = subCategoryList
                print("loadCategorys expenseList \(self.expenseList.count)")
                print("loadCategorys incomeList \(self.incomeList.count)")
            }
        }
    }
    
    func loadSubCategoryList(category: AccountingCategory?, selectedBookId: String? = nil) {
        var bookId = self.bookId
        if selectedBookId != nil {
            bookId = selectedBookId!
        }
        guard let category = category else { return }
        asyncBackground {
            let parentId = category.cid ?? ""
            let subCategoryList = self.categoryDao.getSubCategoryListByParentId(uid: self.uid, bookId: bookId, parentId: parentId)
            self.asyncMain {
                self.subCategoryList = subCategoryList
                // 计算二级类别插入位置
            }
        }
    }
}
